#include <cstdio>
#include <cstring>

#include <algorithm>

using namespace std;

int n, m;
int A[505];
int B[505];

int best[505];
int dp[505][505];

int main( void )
{
	freopen( "input.txt", "r", stdin );

	for( ; scanf( "%d %d", &n, &m ) == 2; ) {
		if( !n && !m ) break;

		for( int i = 0; i < n; ++i ) scanf( "%d", &A[i] );
		for( int i = 0; i < m; ++i ) scanf( "%d", &B[i] );

		memset(dp, 0, sizeof(dp));
		memset(best, 0, sizeof(best));

		int answer = 0;

		for( int i = 0; i < n; ++i ) {
			for( int j = 0; j < m; ++j ) if( A[i] == B[j] ) {
				for( int k = 0; k < j; ++k ) if( B[j] > B[k] )
					dp[i][j] = max(dp[i][j], best[k]);
				answer = max(answer, ++dp[i][j]);
			}
			for( int j = 0; j < m; ++j )
				best[j] = max(best[j], dp[i][j]);
		}

		printf("%d\n", answer);
	}

	return 0;
}
